package com.ggh.jinjilive.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.dialog.LoadDialog;
import com.ggh.jinjilive.mobile.UserMobile;
import com.ggh.jinjilive.remotedata.ViewModelFactory;

import javax.inject.Inject;

public abstract class BaseFragmentActivity<V extends ViewDataBinding> extends FragmentActivity{

    protected Context mContext;
    //布局
    protected V binding;
    @Inject
    protected ViewModelFactory viewModelFactory;
    //用户信息
    protected UserMobile userMobile;
    //
    protected LoadDialog loadDialog;
    //标题
    protected Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        //用户信息
        userMobile = MerchantSession.getInstance().getUser();
        //
        initCreate();
        //页面接受的参数方法
        initParam();
        //私有的初始化Databinding和ViewModel方法
        initViewDataBinding(savedInstanceState);
        //页面初始化方法
        initView();
        //页面数据初始化方法
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();

    }

    public void initCreate(){

    }


    public void initParam() {

    }


    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView(Bundle savedInstanceState);

    /**
     * 注入绑定
     */
    private void initViewDataBinding(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, initContentView(savedInstanceState));
    }

    public void initView() {

    }


    public void initData() {

    }

    public void initViewObservable() {

    }


    public void showDialog() {
        if (!isFinishing()) {
            if(null != loadDialog && loadDialog.isShowing()){
                return;
            }
            loadDialog = new LoadDialog(mContext);
            try {
                loadDialog.show();
            }catch (Exception e){
                e.getMessage();
            }
        }
    }


    public void cancelDialog() {
        if (loadDialog != null && !isFinishing()) {
            loadDialog.dismiss();
        }
    }




    /**
     * 隐藏软键盘
     */
    protected void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
    }


}
